<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<script>
    window.onload = function() {
        var btn = document.getElementById('btn');
        btn.onclick = function() {
            var username = document.getElementById('username').value;
            var pwd = document.getElementById('pwd').value
                // 使用Ajax 发送需要以下几步：
                //1.创建XMLHttpRequest 对象
            var xhr = new XMLHttpRequest();
            //2. 准备发送
            xhr.open('get', './ajaxlogin.php?username=' + username + '&pwd=' + pwd, true);
            //3.执行发送动作
            xhr.send(null);
            //4.指定回调函数
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        var data = xhr.responseText;
                        var info = document.getElementById('info');
                        if (data == '1') {
                            info.innerHTML = '登陆成功';
                        } else if (data == '2') {
                            info.innerHTML = '用户名或者密码错误';
                        }
                    }
                }
            }
        }
    }
</script>

<body>
    <div>
        <form>
            用户名：<input type="text" name="username" id="username"><span id="info"></span><br> 密码：
            <input type="text" name="pwd" id="pwd">
            <input type="submit" value="登录" name="btn" id="btn">
        </form>
    </div>
</body>

</html>